import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCounterStore = defineStore('counter', () => {
    const carts = ref([])
    const id = ref(0)
    function add(data,goodsid) {
        let username=localStorage.getItem('username')
        let usercarts=carts.value.filter(item=>item.username==username)
        let pd=usercarts.find(item=>item.goods.goods_id==goodsid)
        if(pd){
            pd.number++
        }else{
            carts.value.push({
                username:username,
                number:1,
                id:id.value++,
                checked:false,
                goods:data
            })
        }
    }
    function dele(id) {
        carts.value=carts.value.filter(item=>item.id!=id)
    }

    return { carts, add, dele }
},
{
    persist: true,
    },
)